#!/usr/bin/env python
# encoding: utf-8
"""
sampleuploader.py

This application is a simple application that allows you to upload
entire directorie trees.

Created by Barry Ruffner on 2010-09-03.
Copyright (c) 2008-2010 Nirvanix. All rights reserved.
"""

import sys
import os
import os.path
from nirvanix import NirvanixUploader
from nirvanix import Nirvanix
from nirvanix import Session
import nirvanixutils

def main():
	args = sys.argv[1:]
	if len(args) < 5:
		print "\r\nUsage: AppKey Username Password NirvanixPath LocalPath"
		print "Example: python sampleuploader.py XXXX-XXXX-XXXX MyUser MyPassword /uploaddir/ /localdir/\r\n"
		return
	# assign args to nice names
	appKey = args[0]
	username = args[1]
	password = args[2]
	nvxpath = args[3]
	localpath = args[4]
	
	# Create a session object
	try:
		session = Session(appKey, username, password)
	except nirvanixutils.NirvanixException, e:
		if e.code == 80001:
			print "Login Failed, Please correct the username and password you provided and try again."
			return
		else:
			print "The following error occured while logging in: " + e.message

	# Create an uploader object with ShowProgress = true
	try:
		uploader = NirvanixUploader(session, True)
		if os.path.isfile(localpath):
			uploader.uploadfile(localpath, nvxpath, basename(abspath(path)))
		else:
			for root, dirs, files in os.walk(localpath):
				for name in files:
 					uploader.uploadfile(os.path.join(root, name), os.path.join(nvxpath + root), name)
					print "\r\nUploaded: %s to %s" % (os.path.join(root, name), os.path.join(nvxpath + root))
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	
if __name__ == '__main__':
	main()

